package com.iteaj.iot.client.modbus;

import com.iteaj.iot.client.ClientComponent;
import com.iteaj.iot.client.TcpSocketClient;
import com.iteaj.iot.codec.IotMessageDecoder;
import com.iteaj.iot.codec.adapter.LengthFieldBasedFrameMessageDecoderAdapter;
import com.iteaj.iot.config.ConnectProperties;

/**
 *
 * Modbus Tcp协议实现的客户端
 * @see com.iteaj.iot.client.modbus.message.ModbusTcpMessage
 */
public class ModbusTcpClient extends TcpSocketClient {

    private int maxFrameLength;

    /**
     * 默认报文最大长度 1024字节
     * @param clientComponent
     * @param config
     */
    public ModbusTcpClient(ClientComponent clientComponent, ConnectProperties config) {
        this(clientComponent, config, 1024);
    }

    public ModbusTcpClient(ClientComponent clientComponent, ConnectProperties config, int maxFrameLength) {
        super(clientComponent, config);
        this.maxFrameLength = maxFrameLength;
    }

    @Override
    protected IotMessageDecoder createProtocolDecoder() {
        return new LengthFieldBasedFrameMessageDecoderAdapter(this.maxFrameLength, 4, 2, 0, 0, true);
    }
}
